/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import entity.Book;
import entity.Shelf;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sbfec.ShelfFacade;
import sessionBean.GestoreLibri;

/**
 *
 * @author Admin
 */
@WebServlet(name = "BookServlet", urlPatterns = {"/BookServlet"})
public class BookServlet extends HttpServlet {
    @EJB
    private GestoreLibri gestoreLibri;  
    
    private void forward(String where, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher rd = getServletContext().getRequestDispatcher(where);
        rd.forward(request, response);
    }
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            if (request.getParameter("action").equals("show")){
                List<Shelf> shelves = gestoreLibri.getShelves();
                request.setAttribute("shelves", shelves);
                List<Book> books = gestoreLibri.getAllBooks();
                request.setAttribute("books", books);
                printDebug();
                forward("/show.jsp", request, response);
            }
            else if (request.getParameter("action").equals("add")){
                List<Shelf> shelves = gestoreLibri.getShelves();
                request.setAttribute("shelves", shelves);
                printDebug();
                forward("/add.jsp", request, response);
            }
            else if (request.getParameter("action").equals("books")){
                List<Book> books = gestoreLibri.getAllBooks();
                request.setAttribute("books", books);
                printDebug();
                forward("/libri.jsp", request, response);
            }
            else if (request.getParameter("action").equals("reallyAdd")){
                String title = request.getParameter("title");
                String author = request.getParameter("author");
                String shelfS = request.getParameter("shelfS");
                String shelfT = request.getParameter("shelfT");
                String shelf = shelfT.equals("") ? shelfS : shelfT;
                gestoreLibri.addBook(title, author, shelf);
                
                List<Shelf> shelves = gestoreLibri.getShelves();
                request.setAttribute("shelves", shelves);
                List<Book> books = gestoreLibri.getAllBooks();
                request.setAttribute("books", books);
                request.setAttribute("nBooks", gestoreLibri.numberOfBooks());
                forward("/show.jsp", request, response);
            }
            else{
                out.println("<p>What are you trying to do??!</p>");
            }
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void printDebug() {
        List<Shelf> shelves = gestoreLibri.getShelves();
        System.out.println("-----------------DEBUG INFO-------------------");
        for (Shelf s: shelves){
            System.out.println("**** Shelf "+s.getGenre()+" ****");
            List<Book> books = s.getBooks();
            for (Book b: books){
                System.out.println(" - "+b.getAuthor()+": "+b.getName());
            }
        }
        System.out.println("-----------------------------------------------");
    }
}
